﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Domain;

namespace Mengsk.ProxyDelivery.App.UI.Accounting
{
    /// <summary>
    /// WidthdrawUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class WidthdrawUserControl : UserControl, IPlugin
    {
        private SellerWithdrawalService service = ServiceContainer.GetService<SellerWithdrawalService>();
        private List<SellerWithdrawalVM> sellerWithDrawals = null;
        public WidthdrawUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "提现管理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbStates.Bind<SellerWithdrawsState>();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.pbBar.Parameters.Clear();
            this.pbBar.Parameters.Add("state", this.cbbStates.SelectedIndex);
            int sellerId = 0;
            int.TryParse(this.tbSellerId.Text, out sellerId);
            this.pbBar.Parameters.Add("sellerId", sellerId);
            this.pbBar.Parameters.Add("op", this.tbCompleteOperator.Text.Trim());
            this.pbBar.Parameters.Add("comment", this.tbComment.Text.Trim());
            this.pbBar.Parameters.Add("startCreateTime", this.dpStartCreateTime.SelectedDate == null ? DateTime.MinValue : this.dpStartCreateTime.SelectedDate.Value.Date);
            this.pbBar.Parameters.Add("endCreateTime", this.dpEndCreateTime.SelectedDate == null ? DateTime.MinValue : this.dpEndCreateTime.SelectedDate.Value.Date.AddDays(1));
            this.pbBar.StartPage();
        }

        private SellerWithdrawalVM[] GetSelelctedItems()
        {
            if (this.sellerWithDrawals == null || this.sellerWithDrawals.Count < 1)
            {
                MessageBox.Show("请选择要处理的提现记录");
                return new SellerWithdrawalVM[0];
            }

            var selected = this.sellerWithDrawals.Where(obj => obj.IsChecked).ToArray();
            if (selected.Length < 1)
            {
                MessageBox.Show("请选择要处理的提现记录");
                return new SellerWithdrawalVM[0];
            }

            return selected;
        }


        private void btnAccept_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var items = this.GetSelelctedItems();
                if (items == null || items.Length < 1)
                {
                    return;
                }
                foreach (var item in items)
                {
                    this.service.Review(item.Source.Id, item.Source.SellerId, true, "");
                    item.Source.State = item.State = SellerWithdrawsState.WaitWithdraws;
                }
                MessageBox.Show("已完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnReject_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var items = this.GetSelelctedItems();
                if (items == null || items.Length < 1)
                {
                    return;
                }

                var window = new MessageInput { Title = "输入拒绝原因", InputContent = "" };
                bool? ret = window.ShowDialog();
                if (ret == false || ret.Value == false)
                {
                    return;
                }
                foreach (var item in items)
                {
                    this.service.Review(item.Source.Id, item.Source.SellerId, false, window.InputContent);
                }
                MessageBox.Show("已完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnComplete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var items = this.dgvItems.SelectedItem as SellerWithdrawalVM;
                if (items == null)
                {
                    MessageBox.Show("请选择一个提现记录");
                    return;
                }

                var window = new SellerWithdrawalWindow { SellerWithdrawal = items };
                bool? ret = window.ShowDialog();
                if (ret != null && ret.Value)
                {
                    this.dgvItems.ItemsSource = null;
                    this.dgvItems.ItemsSource = this.sellerWithDrawals;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var items = this.GetSelelctedItems();
                if (items == null || items.Length < 1)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var item = this.GetSelelctedItems();
                if (item == null || item.Length < 1)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = this.service.GetByAll(e.GetParameter<int>("sellerId"), e.GetParameter<string>("op"), e.GetParameter<string>("comment"), e.GetParameter<SellerWithdrawsState>("state"), e.GetParameter<DateTime>("startCreateTime"), e.GetParameter<DateTime>("endCreateTime"), e.CurrentPage - 1, e.PageSize);
            this.pbBar.Total = data.Total;
            this.pbBar.CurrentCount = data.Datas.Count;
            this.sellerWithDrawals = data.Datas.Select(obj => new SellerWithdrawalVM(obj)).ToList();
            this.tbTotal.Text = "当前页金额:" + data.Datas.Select(obj => obj.Money).Sum();
            this.dgvItems.ItemsSource = this.sellerWithDrawals;

        }
    }
}
